
import com.adobe.ep.notifications.Notification;
import com.adobe.ep.notifications.NotificationAlertPolicy;
import com.adobe.ep.notifications.NotificationEvent;
import com.adobe.ep.notifications.NotificationIconType;
import com.adobe.ep.notifications.NotificationManager;

import flash.events.MouseEvent;
import flash.events.StatusEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

public var notificationManager:NotificationManager = new NotificationManager();
public var noti:Notification;

// You can set this value to anything you want (and use various values)
// You can then pass this value into the notification manager to cancel notifications of a specified type
public var NOTIFICATION_CODE:String = "MY_CUSTOM_NOTIFICATION";

public var timedNotification:Timer;


private var newNotiCount:int;

protected function activateNotification(nCount:int):void{
	if ( notificationManager.isSupported ){
		trace("noti ok");
		newNotiCount = nCount;
		createManager();
	}else{
		trace("The notification native extension is not supported.");
	}
}


protected function expireLocalNotiStatus():void{
	if ( notificationManager.isSupported ){
		trace("noti ok");
		deleteManager();
	}else{
		trace("The notification native extension is not supported.");
	}
}


//setup notification manager
protected function createManager():void
{
	//notificationManager = new NotificationManager();
	
	noti = new com.adobe.ep.notifications.Notification();
	
	// This action label appears in two places on iOS: on the action button of the notification dialog, and on the unlock slider when the device is locked.
	noti.actionLabel = "Open";// If unset, the iOS default will be used.
	
	// Allows you to contorl whether an alert is dispathed with each notification, or just the first notification.
	noti.alertPolicy = NotificationAlertPolicy.FIRST_NOTIFICATION;
	
	// On Android, specifies whether the notification persists when the user taps it in the notification area
	noti.cancelOnSelect = true;
	
	// On Android and iOS "...the app will be brought to the foreground if it was in the background or launched if it had been shutdown.
	// On iOS, the way to perform the action of a notification manifests itself as a button 
	// on the notification dialog that appears when a notification is fired and different
	// text on the unlock slider when the device is locked. 
	// On Android, the way to perform an action is not visible, it is performed by selecting 
	// the notification from the notification list (window shade)."
	noti.hasAction = true;
	noti.actionData = "customAction";
	
	// Allows you to set the Android notification icon
	noti.iconType = NotificationIconType.INFO;
	
	// On both Android and iOS, lets you set a number on the icon or application badge
	noti.numberAnnotation = newNotiCount;
	
	// On Android, "ongoing" notifications aren't cleared with the clear button
	noti.ongoing = false;
	
	// Android and iOS
	noti.playSound = true;
	
	// On Android, alerts (sound and vibration) until acknowledged 
	noti.repeatAlertUntilAcknowledged = false; // true->false 변경함.
	
	// Only configurable on Android
	noti.vibrate = false;
	
	noti.tickerText = "트윗애드온즈 알림";
	noti.title = "트윗애드온즈";
	noti.body = newNotiCount + "개의 새로운 알림이 도착했습니다!";
	
	//user touches notification
	this.notificationManager.addEventListener(NotificationEvent.NOTIFICATION_ACTION,onNotificationActionEvent);
	this.notificationManager.addEventListener(StatusEvent.STATUS,onNotificationStatusEvent);
	
	//start timer to send notification
	startTimer();
	
	//setup buttons
	//notify_btn.addEventListener(MouseEvent.CLICK, notify_ClickHandler);
	//cancel_btn.addEventListener(MouseEvent.CLICK, cancelNotification_clickHandler);
	
	notifyUser();
}


//setup notification manager
protected function deleteManager():void
{
	//notificationManager = new NotificationManager();
	
	noti = new com.adobe.ep.notifications.Notification();
	
	// This action label appears in two places on iOS: on the action button of the notification dialog, and on the unlock slider when the device is locked.
	noti.actionLabel = "Open";// If unset, the iOS default will be used.
	
	// Allows you to contorl whether an alert is dispathed with each notification, or just the first notification.
	noti.alertPolicy = NotificationAlertPolicy.EACH_NOTIFICATION;
	
	// On Android, specifies whether the notification persists when the user taps it in the notification area
	noti.cancelOnSelect = true;
	
	// On Android and iOS "...the app will be brought to the foreground if it was in the background or launched if it had been shutdown.
	// On iOS, the way to perform the action of a notification manifests itself as a button 
	// on the notification dialog that appears when a notification is fired and different
	// text on the unlock slider when the device is locked. 
	// On Android, the way to perform an action is not visible, it is performed by selecting 
	// the notification from the notification list (window shade)."
	noti.hasAction = true;
	noti.actionData = "customAction";
	
	// Allows you to set the Android notification icon
	noti.iconType = NotificationIconType.MESSAGE;
	
	// On both Android and iOS, lets you set a number on the icon or application badge
	noti.numberAnnotation = 0;
	
	// On Android, "ongoing" notifications aren't cleared with the clear button
	noti.ongoing = false;
	
	// Android and iOS
	noti.playSound = false;
	
	// On Android, alerts (sound and vibration) until acknowledged 
	noti.repeatAlertUntilAcknowledged = false; // true->false 변경함.
	
	// Only configurable on Android
	noti.vibrate = false;
	
	/*
	noti.tickerText = "트윗애드온즈 알림";
	noti.title = "트윗애드온즈";
	noti.body = newNotiCount + "개의 새로운 알림이 도착했습니다!";
	*/
	
	//user touches notification
	this.notificationManager.addEventListener(NotificationEvent.NOTIFICATION_ACTION,onNotificationActionEvent);
	this.notificationManager.addEventListener(StatusEvent.STATUS,onNotificationStatusEvent);
	
	//start timer to send notification
	startTimer();
	
	//setup buttons
	//notify_btn.addEventListener(MouseEvent.CLICK, notify_ClickHandler);
	//cancel_btn.addEventListener(MouseEvent.CLICK, cancelNotification_clickHandler);
	
	notifyUser();
}
	
//send notification
	
	
/* notify button
public function notify_ClickHandler():void{
	notifyUser();
}
*/

protected function notifyUser():void
{	
	// Dispatch the notification
	// Note that the way popups work on iOS dictate that a pop-up window will only appear when the application is in the background. 
	// If the notification is dispatched and the app is running normally, you won't see a pop-up (but you will receive the event).
	this.notificationManager.notifyUser(NOTIFICATION_CODE,noti);
	trace("air notification sent");   
}
	
//cancel button
protected function cancelNotification_clickHandler(event:MouseEvent):void
{
	if ( this.notificationManager )
	{					
		this.notificationManager.cancel(NOTIFICATION_CODE);
		trace("Notification canceled.");
	}				
	else
	{
		trace("No notifications have been dispatched.");					
	}
}

//user touches notification
private function onNotificationActionEvent(ne:NotificationEvent):void
{            
	trace("Notification action received. Type: " + ne.actionData);
}

private function onNotificationStatusEvent(ne:StatusEvent):void
{
	trace("Notification status event received!");
}

//start timer notification
protected function startTimer():void{
	timedNotification = new Timer(5000,1)
	timedNotification.start();
	timedNotification.addEventListener(TimerEvent.TIMER, callNotifyUser);
}

//timer event
protected function callNotifyUser(event:TimerEvent):void{
	notifyUser();
	timedNotification.removeEventListener(TimerEvent.TIMER, callNotifyUser);
	timedNotification.stop();
}
